package com.baiyangtx.lingerweather.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

public abstract class AbstractSyncTask implements SyncTask{

	

	private Map<String, String> city;

	protected abstract Parser getParser();

	protected abstract String getUrl();

	@Override
	public Map<String, String> syncCity() throws MalformedURLException, IOException {
		
		String url = getUrl();

		HttpURLConnection connection;

		connection = (HttpURLConnection) new URL(url).openConnection();
		connection.setRequestMethod("GET");
		connection.connect();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));

		String ss = null;
		StringBuilder builder = new StringBuilder();
		while ((ss = reader.readLine()) != null) {
			builder.append(ss);
		}
		builder.delete(0, 16);
		builder.delete(builder.length() - 2, builder.length());

		Parser parser = getParser();
		parser.setText(builder.toString()) ;
		city = parser.parser();

		return city;
	}

}
